import numpy as np

if __name__ == "__main__":
    # 矩阵
    # a = np.asmatrix([[1, 2, 3], [2, 4, 9], [7, 7, 7]])
    A = np.mat([[1 + 1j, 2 + 1j, 3 + 1j], [2 + 1j, 4 + 1j, 9 + 1j], [7 + 1j, 7 + 1j, 7 + 1j]])
    # a = np.matrix("1+i, 2+i, 3+i;2+i, 4+i, 9+i;7+i, 7+i, 7+i")
    b = np.array([1, 1, 2])
    print(A)
    print(A * 4)
    print(A * A)
    print(A ** 4)
    # 求Ax = b  ,A 为非奇异矩阵  non-singular matrix
    x = np.linalg.solve(A, b)
    print(x)
    # 矩阵的点积
    np.dot(A, b)
    np.linalg.multi_dot([A, x])
    # 共轭
    A.conjugate()
    # 转置
    A.transpose()
    # 迹
    A.trace()
    # 特征值
    A_eigenvalue = np.linalg.eig(A)[0]
    print(A_eigenvalue.sum() == A.trace())
    print('特征值sum:{},迹：{}'.format(A_eigenvalue.sum(), A.trace()))

    a = np.arange(1, 4)
    b = np.arange(1, 10).reshape(3, 3)
    # 对角矩阵
    np.diag(a)
    # 对角线元素
    np.diag(b)
    # 矩阵的上三角 Upper triangle
    np.triu(b)
    # 矩阵的下三角 Lower triangle  parameter k : Diagonal above which to zero elements.
    np.tril(b, 1)
    A = np.mat([[1, 1, -1], [2, 1, 1], [3, 2, 0]])
    F = np.mat([[1, 1], [2, 1], [3, 2]])
    G = np.mat([[1, 0, 2], [0, 1, -3]])
    N = np.mat([[16, 6], [9, 6]])
    # print(42 * (G.conjugate().transpose() * (
    #         F.conjugate().transpose() * A * G.conjugate().transpose()).I * F.conjugate().transpose()))
    # print(42 * N.I)
    M = np.mat([[1, 1, -1], [2, 1, 1], [3, 2, 0]])
    N = np.mat([[0, 6, 6], [7, -2, 5.], [-21, 18, -3]])
    G = np.mat([[1, 2, 3]]).transpose()
    print(1/42 * M * N * G)
    print(1/42 * N * M * G)
    print(N * G)
